<template>
  <div class="personal-name">
    <!-- 头部 -->
    <van-nav-bar
      title="设置姓名"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />

    <!-- 输入框 -->
    <div class="filed-wrap">
      <van-field
        v-model.trim="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="20"
        placeholder="请输入姓名"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserInfo } from '@/api/user' // 修改用户信息

export default {
  name: 'PersonalName',
  props: {
    value: {
      type: String,
      required: true
    },
    userId: {
      type: [Number, String],
      required: true
    }
  },
  data () {
    return {
      localName: this.value
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onClickRight () {
      try {
        const localName = this.localName
        // console.log(localName)
        if (!localName.length) return this.$toast('姓名不能为空')

        await updateUserInfo({
          ...this.$store.state.user,
          nickname: localName,
          id: this.userId
        })

        // 更新视图
        this.$emit('input', localName)
        // 关闭弹层
        this.$emit('close')
        // 提示成功
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.personal-name {
  .filed-wrap {
    padding: 10px;
  }
}
</style>
